﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKExcelFormTool.Forms;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Designer
{
	internal class ViewControler
	{
		public ViewControler(ExternalCommandData cmdData, ViewControler.TableViewType tableViewType)
		{
			this.m_Revit = cmdData;
			this.m_TableViewType = tableViewType;
			switch (tableViewType)
			{
			case ViewControler.TableViewType.TVT_GENERAL:
				this.m_Prefix = "统计表";
				return;
			case ViewControler.TableViewType.TVT_DOORWINDOWDETAILEDSTATEMENT:
				this.m_Prefix = "门窗表";
				return;
			case ViewControler.TableViewType.TVT_DOORWINDOWLEGEND:
				this.m_Prefix = "门窗图例表";
				return;
			case ViewControler.TableViewType.TVT_GRAPHPAPERDIRECTORY:
				this.m_Prefix = "图纸目录表";
				return;
			case ViewControler.TableViewType.TVT_MATERIALTABLE:
				this.m_Prefix = "材料表";
				return;
			case ViewControler.TableViewType.TVT_IMPORTEXCELTABLE:
				this.m_Prefix = "Excel导入表格";
				return;
			case ViewControler.TableViewType.TVT_CABLETRENCHSECTIONVIEW:
				this.m_Prefix = "电缆沟剖面";
				return;
			case ViewControler.TableViewType.TVT_GRIDDRAW:
				break;
			case ViewControler.TableViewType.TVT_RampExpandedView:
				this.m_Prefix = "坡道展开图";
				break;
			default:
				return;
			}
		}

		public ViewControler(ExternalCommandData cmdData, ViewControler.TableViewType tableViewType, string tableName)
		{
			this.m_Revit = cmdData;
			this.m_TableViewType = tableViewType;
			switch (tableViewType)
			{
			case ViewControler.TableViewType.TVT_GENERAL:
				this.m_Prefix = "统计表";
				return;
			case ViewControler.TableViewType.TVT_DOORWINDOWDETAILEDSTATEMENT:
				this.m_Prefix = "门窗表";
				return;
			case ViewControler.TableViewType.TVT_DOORWINDOWLEGEND:
				this.m_Prefix = "门窗图例表";
				return;
			case ViewControler.TableViewType.TVT_GRAPHPAPERDIRECTORY:
				this.m_Prefix = "图纸目录表";
				return;
			case ViewControler.TableViewType.TVT_MATERIALTABLE:
				this.m_Prefix = "材料表";
				return;
			case ViewControler.TableViewType.TVT_IMPORTEXCELTABLE:
				this.m_Prefix = "Excel导入表格";
				return;
			case ViewControler.TableViewType.TVT_CABLETRENCHSECTIONVIEW:
				this.m_Prefix = "电缆沟剖面";
				return;
			case ViewControler.TableViewType.TVT_GRIDDRAW:
				this.m_Prefix = tableName;
				return;
			case ViewControler.TableViewType.TVT_RampExpandedView:
				this.m_Prefix = "坡道展开图";
				return;
			default:
				return;
			}
		}

		public ViewSelectResult CreateDraftingView(int viewScale = 100)
		{
			string newName = this.GetNewDraftingViewName();
			List<ViewDrafting> draftingViewList = this.GetDraftingViewList();
			List<string> list = new List<string>();
			foreach (ViewDrafting viewDrafting in draftingViewList)
			{
				list.Add(viewDrafting.Name);
			}
			ViewSelectionForm viewSelectionForm = new ViewSelectionForm(newName, this.m_Prefix, list, this.m_Revit);
			DialogResult dialogResult = viewSelectionForm.ShowDialog();
			if (dialogResult == DialogResult.OK)
			{
				if (viewSelectionForm.IsNewName || viewSelectionForm.SelectViewIndex == -1)
				{
					newName = viewSelectionForm.NewName;
					Document document = this.m_Revit.Application.ActiveUIDocument.Document;
					Transaction transaction = new Transaction(document);
					transaction.Start("creat view drafting");
					ViewDrafting viewDrafting2 = null;
					if (!Common.CreateViewDrafting(document, viewScale, out viewDrafting2))
					{
						transaction.RollBack();
						return ViewSelectResult.VSR_Cancel;
					}
					this.SetDraftingViewName(viewDrafting2, newName);
					transaction.Commit();
					this.m_Revit.Application.ActiveUIDocument.ActiveView = viewDrafting2;
					UIDocument activeUIDocument = this.m_Revit.Application.ActiveUIDocument;
					activeUIDocument.RefreshActiveView();
					UIView uiview = null;
					foreach (UIView uiview2 in activeUIDocument.GetOpenUIViews())
					{
						if (uiview2.ViewId.Equals(activeUIDocument.ActiveView.Id))
						{
							uiview = uiview2;
							break;
						}
					}
					if (uiview != null)
					{
						XYZ xyz = new XYZ(-100.0, -100.0, -5.0);
						XYZ xyz2 = new XYZ(100.0, 100.0, 5.0);
						uiview.ZoomAndCenterRectangle(xyz, xyz2);
					}
				}
				else
				{
					int selectViewIndex = viewSelectionForm.SelectViewIndex;
					this.m_Revit.Application.ActiveUIDocument.ActiveView = draftingViewList[selectViewIndex];
				}
				return ViewSelectResult.VSR_UserSelect;
			}
			if (dialogResult == DialogResult.Abort)
			{
				return ViewSelectResult.VSR_Current;
			}
			return ViewSelectResult.VSR_Cancel;
		}

		private void SetDraftingViewName(ViewDrafting view, string newName)
		{
			string name = view.Name;
			try
			{
				view.Name = newName;
			}
			catch (Exception)
			{
				view.Name = name;
			}
		}

		private string GetNewDraftingViewName()
		{
			string res = "";
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(ViewDrafting));
			List<string> list = new List<string>();
			foreach (Element element in filteredElementCollector)
			{
				ViewDrafting viewDrafting = element as ViewDrafting;
				if (viewDrafting != null)
				{
					string name = viewDrafting.Name;
					if (name.IndexOf(this.m_Prefix) != -1)
					{
						list.Add(name);
					}
				}
			}
			int num = 0;
			Predicate<string> ss9__0 = null;
			for (;;)
			{
				res = this.m_Prefix + "_" + (num + 1).ToString();
				List<string> list2 = list;
				Predicate<string> match;
				if ((match = ss9__0) == null)
				{
					match = (ss9__0 = ((string s) => s.CompareTo(res) == 0));
				}
				if (list2.FindIndex(match) == -1)
				{
					break;
				}
				num++;
			}
			return res;
		}

		private List<ViewDrafting> GetDraftingViewList()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(ViewDrafting));
			List<ViewDrafting> list = new List<ViewDrafting>();
			foreach (Element element in filteredElementCollector)
			{
				ViewDrafting viewDrafting = element as ViewDrafting;
				if (viewDrafting != null && viewDrafting.Name.IndexOf(this.m_Prefix) != -1)
				{
					list.Add(viewDrafting);
				}
			}
			return list;
		}

		public bool CreateNewDraftingView(int viewScale = 100)
		{
			string newName = this.GetNewDraftingViewName();
			List<ViewDrafting> draftingViewList = this.GetDraftingViewList();
			List<string> list = new List<string>();
			foreach (ViewDrafting viewDrafting in draftingViewList)
			{
				list.Add(viewDrafting.Name);
			}
			if (list.IndexOf(this.m_Prefix) != -1)
			{
				this.m_Prefix += "_1";
			}
			newName = this.m_Prefix;
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			Transaction transaction = new Transaction(document);
			transaction.Start("creat view drafting");
			ViewDrafting viewDrafting2 = null;
			if (!Common.CreateViewDrafting(document, viewScale, out viewDrafting2))
			{
				transaction.RollBack();
				return false;
			}
			this.SetDraftingViewName(viewDrafting2, newName);
			transaction.Commit();
			this.m_Revit.Application.ActiveUIDocument.ActiveView = viewDrafting2;
			UIDocument activeUIDocument = this.m_Revit.Application.ActiveUIDocument;
			activeUIDocument.RefreshActiveView();
			UIView uiview = null;
			foreach (UIView uiview2 in activeUIDocument.GetOpenUIViews())
			{
				if (uiview2.ViewId.Equals(activeUIDocument.ActiveView.Id))
				{
					uiview = uiview2;
					break;
				}
			}
			if (uiview != null)
			{
				XYZ xyz = new XYZ(-100.0, -100.0, -5.0);
				XYZ xyz2 = new XYZ(100.0, 100.0, 5.0);
				uiview.ZoomAndCenterRectangle(xyz, xyz2);
			}
			return true;
		}

		private ExternalCommandData m_Revit;

		private ViewControler.TableViewType m_TableViewType = ViewControler.TableViewType.TVT_IMPORTEXCELTABLE;

		private string m_Prefix = "";

		public enum TableViewType
		{
			TVT_GENERAL,
			TVT_DOORWINDOWDETAILEDSTATEMENT,
			TVT_DOORWINDOWLEGEND,
			TVT_GRAPHPAPERDIRECTORY,
			TVT_MATERIALTABLE,
			TVT_IMPORTEXCELTABLE,
			TVT_CABLETRENCHSECTIONVIEW,
			TVT_GRIDDRAW,
			TVT_RampExpandedView
		}
	}
}
